
import { GetCartListData,ChangeGoodChecked,StepperChangeData,DeleteGood } from '../api'

const actions={
  // 获取购物车商品
  async getCartListData({commit}){
    const result=await GetCartListData()
    // console.log('购物车信息',result)
    if(result.errno===0){
      commit('SAVE_CART_LIST_DATA',result.data)
    }else{
      alert(result.message)
    }
  },
  // 选中
  async gethangeChecked({commit},params){
    const result=await ChangeGoodChecked(params)
    if(result.errno===0){
      commit('SAVE_CART_LIST_DATA',result.data)
    }else{
      alert(result.message)
    }
  },
  // 商品步进器
  async getStepperChange({commit},params){
    if(params.number>100){
      params.number = 100
    }
    const result =await StepperChangeData(params)
    console.log(result,'ssss')
    if(result.errno===0){
      commit('SAVE_CART_LIST_DATA',result.data)
    }else{
      alert(result.message)
    }
  },
  // 删除
  async getDeleteGood({commit},params){
    const result =await DeleteGood(params)
    console.log(result,'ddd')
    if(result.errno===0){
      commit('SAVE_CART_LIST_DATA',result.data)
    }else{
      alert(result.message)
    }
  }
}
const mutations={
  SAVE_CART_LIST_DATA(start,info){
    start.cartInfo=info
  }
}
const state={
  cartInfo:{
    cartList:[],
    cartTotal:{}
  },
}
const getters={
  cartList(state){
    return state.cartInfo.cartList
  },
  cartTotal(state){
    return state.cartInfo.cartTotal
  }
}
export default{
  namespaced:true,
  actions,
  mutations,
  state,
  getters
}